package com.duan.filter;


import com.duan.pojo.User;
import com.duan.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        //过滤器从Session中获取用户
        User user = (User)request.getSession().getAttribute(Constants.USER_SESSION);

        if(user == null){
            System.out.println("用户已经被移除了，马上重定向到错误页面");
            //已经被移出/注销了,或者未登录
            System.out.println(request.getRequestURI());
            response.sendRedirect(request.getContextPath() + "/error.jsp");//"/error.jsp"不行
        }else{
            chain.doFilter(req, resp);
        }
    }

    public void destroy() {

    }
}
